package com.cytech.cloudclinic.moudle.main.clinic.admission;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.app.Constant;
import com.cytech.cloudclinic.base.BaseFragment;
import com.cytech.cloudclinic.base.BasePresenter;
import com.cytech.cloudclinic.bean.Patient;
import com.cytech.cloudclinic.bean.req.AddPrescription;
import com.cytech.cloudclinic.moudle.main.clinic.admission.diagnosis.DiagnosisFragment;
import com.cytech.cloudclinic.moudle.main.clinic.admission.intelligent.IntelligentFragment;
import com.cytech.cloudclinic.moudle.main.clinic.admission.prescription.PrescriptionFragment;
import com.cytech.cloudclinic.moudle.main.clinic.admission.report.ReportRecordFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class AdmissionStyleFragment extends BaseFragment {
    @BindView(R.id.rg_admission_type)
    RadioGroup typeRg;
    @BindView(R.id.rb_admission_child1)
    RadioButton child1;
    @BindView(R.id.rb_admission_child2)
    RadioButton child2;
    @BindView(R.id.rb_admission_child3)
    RadioButton child3;
    @BindView(R.id.rb_admission_child4)
    RadioButton child4;

    private int currentFragmentIndex = 0;
    private List<Fragment> fragmentList;
    private Fragment currentFragment;
    private Fragment fragment1;
    private Fragment fragment2;
    private Fragment fragment3;
    private Fragment fragment4;
    private int drugTagId;
    private int patinetId;
    private Patient patient;

    public static AdmissionStyleFragment newInstance(int drugTagId,int patientId,Patient patient) {
        AdmissionStyleFragment fragment = new AdmissionStyleFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("drugTagId",drugTagId);
        bundle.putInt("patientId",patientId);
        bundle.putSerializable("patient",patient);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_admission_container;
    }

    @Override
    protected void initView() {
        drugTagId = getArguments().getInt("drugTagId");
        patinetId = getArguments().getInt("patientId");
        patient = (Patient) getArguments().getSerializable("patient");
        fragmentList = new ArrayList<>();
        if(drugTagId == Constant.DRUG_TAG_6){
            child4.setVisibility(View.GONE);
            child1.setText("1.主诉与诊断");
            child2.setText("2.查体记录及图片");
            child3.setText("3.处方");

            fragment2 = DiagnosisFragment.newInstance(drugTagId);
            fragment3 = ReportRecordFragment.newInstance(null);
            fragment4 = PrescriptionFragment.newInstance(drugTagId,null,patient);

            fragmentList.add(fragment2);
            fragmentList.add(fragment3);
            fragmentList.add(fragment4);
        }else{
            fragment1 = IntelligentFragment.newInstance();
            fragment2 = DiagnosisFragment.newInstance(drugTagId);
            fragment3 = ReportRecordFragment.newInstance(null);
            fragment4 = PrescriptionFragment.newInstance(drugTagId,null,patient);

            fragmentList.add(fragment1);
            fragmentList.add(fragment2);
            fragmentList.add(fragment3);
            fragmentList.add(fragment4);

        }

        switchFragment(0);

        typeRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_admission_child1:
                        switchFragment(0);
                        break;
                    case R.id.rb_admission_child2:
                        switchFragment(1);
                        break;
                    case R.id.rb_admission_child3:
                        switchFragment(2);
                        break;
                    case R.id.rb_admission_child4:
                        switchFragment(3);
                }
            }
        });

    }

    @Override
    protected void initData() {

    }

    private void switchFragment(int position) {
        Log.d("TAG", "switchFragment: "+position);
        currentFragmentIndex = position;
        Fragment targetFragment = fragmentList.get(position);
        FragmentTransaction transaction = getChildFragmentManager()
                .beginTransaction();
        if (!targetFragment.isAdded()) {
            if (currentFragment != null) {
                transaction.hide(currentFragment);
            }
            transaction.add(R.id.admission_child_container, targetFragment).commit();
            //System.out.println("还没添加呢");
        } else {
            transaction
                    .hide(currentFragment)
                    .show(targetFragment)
                    .commit();
            //System.out.println("添加了( ⊙o⊙ )哇");
        }
        currentFragment = targetFragment;
    }

    public Fragment getReportFragment() {
        return fragment3;
    }

    //切换到报告单
    public void switchReportFragment(AddPrescription prescription){
        prescription.patient_id = patinetId;
        prescription.drug_tag = drugTagId;
        fragment3.getArguments().putSerializable("prescription",prescription);
        child3.setChecked(true);
    }

    //切换到处方
    public void switchPrescriptionFragment(AddPrescription prescription){
        prescription.patient_id = patinetId;
        prescription.drug_tag = drugTagId;
        fragment4.getArguments().putSerializable("prescription",prescription);
        child4.setChecked(true);
    }

}
